package hibernate.util;


import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

public class HibernateUtil{ 
	
	/*public static Session getSession(){
		
		Session session = null;
		Configuration config = new Configuration().configure();
		StandardServiceRegistry serviceRegistry = null;
		StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder();
		builder.applySettings(config.getProperties());
		serviceRegistry=builder.build();
		SessionFactory sessFactory=config.buildSessionFactory(serviceRegistry);
		session = sessFactory.openSession();
		return session;
		
	}
	
	public static void closeSession (Session session){
		if (session!=null){
			try {
				session.close();
			} catch (HibernateException e) {
				System.err.println("SESSION ERROR: "+e.toString());
			}
		}
	}*/
		private static final SessionFactory sessFac = buildSessionFactory();
		@SuppressWarnings("deprecation")
		private static SessionFactory buildSessionFactory(){
			try {
				return new Configuration().configure("/hibernate/util/hibernate.cfg.xml").buildSessionFactory();
			} catch (Throwable ex) {
				System.err.println("Initial SessionFactory creation failed." +ex);
				throw new ExceptionInInitializerError(ex);
			}
		}
		
		public static SessionFactory getSessionFactory(){
			return sessFac;
		}
		
		public static void shutdown(){
			getSessionFactory().close();
		}
}
